
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_HUD.lua")
AddCSLuaFile("cl_inventory.lua")
AddCSLuaFile("cl_inventory_functions.lua")

include("entity.lua")
include("player.lua")
include("shared.lua")
include("buttonhook.lua")
include("stamina.lua")
include("inventoryconcommand.lua")
include("doors.lua")
include("chat.lua")


GM.Name = "Resource Rp"
GM.Author = "Mr Fix It"
GM.Email = "mrfixit@loungeGN.net"

local playertable = FindMetaTable("Player")
	function playertable:ChatNotify(text)
		self:SendLua("GAMEMODE:AddNotify(\"" .. text .. "\", NOTIFY_CLEANUP, 5);")
	end


function sendInventory(ply,command,arguments)
	umsg.Start("showInventory",ply)
	umsg.Long(table.Count(ply.inventory))		
		for k,v in pairs(ply.inventory) do
			umsg.String(k)
			umsg.Long(v)
		end
	umsg.End()
end

function GM:ShowHelp(ply)
	sendInventory(ply,nil,"help")
end

function spawnMelon(which)
	if(ents.FindByName("farm_melon_"..which)==nil) then print("farm_melon_"..which.." does not exist") return end
	
	for k,v in pairs(ents.FindInSphere(ents.FindByName("farm_melon_"..which)[1]:GetPos(), 200)) do
		if(v:GetName()=="melon_spawned_"..which) then return end
	end
	
	local melon= ents.Create("melon")
	melon:SetPos(ents.FindByName("farm_melon_"..which)[1]:GetPos())
	melon:SetName("melon_spawned_"..which)
	melon:Spawn()

end

timer.Simple(5, function()
	print("settingupmelons")
	for k,v in pairs(ents.GetAll()) do 
		if string.find(v:GetName(), "farm_melon") then
			local num =tonumber(string.Replace(v:GetName(),"farm_melon_",""))
			print("setupmelon"..num..k)
			
			timer.Simple(math.random(0,CfgVars["melon_spawn"]), function()
				spawnMelon(num)
				timer.Create("melonspawner_"..num,CfgVars["melon_spawn"],0,function() 
					spawnMelon(num)
				 end)
			end )
			
		end
	end
end)

function startWelfare(ply)
	ply.welfare=0
	
	timer.Create( "welfare"..ply:GetName(), CfgVars["welfareinterval"], 0, function() 
		if(!ply:IsValid()) then timer.Destroy("welfare"..ply:GetName()) end
		if(ply.welfare<CfgVars["welfaremax"]) then
			ply.welfare=ply.welfare+CfgVars["welfareinc"]
		end
	end )
	
end

function startHunger(ply)
	timer.Create("hunger"..ply:GetName(), CfgVars["hungerinterval"], 0, function() 
		if(!ply:IsValid()) then timer.Destroy("hunger"..ply:GetName()) end
		
		local hunger = ply:GetNWInt("Hunger")
		ply:SetNWInt("Hunger", hunger - CfgVars["hungerdec"])
		
		-- Add check here: if hunger <= 0
	end )
	
end

function AddDir(dir) // recursively adds everything in a directory to be downloaded by client
	local list = file.FindDir("../"..dir.."/*")
	for _, fdir in pairs(list) do
		if fdir != ".svn" then // don't spam people with useless .svn folders
			AddDir(fdir)
		end
	end
 
	for k,v in pairs(file.Find("../"..dir.."/*")) do
   		resource.AddFile(dir.."/"..v)
	end
end 

AddDir("gamemodes/RRP/content/materials/models/weapons")  
AddDir("gamemodes/RRP/content/models/weapons")  